package view.manager;

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import base.NumberGeneration;
import dao.ClassinformationDao;
import dao.CourseDao;
import dao.MajorDao;
import dao.ManagerDao;
import model.Classinformation;
import model.Major;
import model.Manager;
import util.StringUtil;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ClassEditInterfrm extends JInternalFrame {
	private JTextField textField;
	private JTable table;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;

	private JComboBox comboBox;
	private JComboBox comboBox_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClassEditInterfrm frame = new ClassEditInterfrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ClassEditInterfrm() {
		setClosable(true);
		setIconifiable(true);
		setTitle("班级管理");
		setBounds(100, 100, 851, 558);
		getContentPane().setLayout(null);

		JLabel lblNewLabel = new JLabel("班级名称：");
		lblNewLabel.setBounds(106, 23, 87, 15);
		getContentPane().add(lblNewLabel);

		textField = new JTextField();
		textField.setBounds(203, 20, 155, 21);
		getContentPane().add(textField);
		textField.setColumns(10);

		JButton btnNewButton = new JButton("查询");
		btnNewButton.setIcon(new ImageIcon(ClassEditInterfrm.class.getResource("/images/select.png")));
		btnNewButton.setBounds(405, 19, 93, 23);
		getContentPane().add(btnNewButton);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(50, 72, 693, 179);
		getContentPane().add(scrollPane);

		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				/**
				 * b表格点击事件
				 */
				comboBox.removeAllItems();
				comboBox_1.removeAllItems();
				int row = table.getSelectedRow();
				textField_1.setText(table.getValueAt(row, 0).toString());
				fillMajorType(table.getValueAt(row, 1).toString());
				textField_2.setText(table.getValueAt(row, 2).toString());
				fillGradeType(Integer.parseInt(table.getValueAt(row, 3).toString()));
				textField_3.setText(table.getValueAt(row, 4).toString());

			}
		});
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null},
			},
			new String[] {
				"\u73ED\u7EA7\u53F7", "\u6240\u5728\u7CFB", "\u73ED\u7EA7\u540D\u79F0", "\u5E74\u7EA7", "\u4EBA\u6570"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		scrollPane.setViewportView(table);

		JPanel panel = new JPanel();
		panel.setBorder(
				new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(50, 289, 693, 206);
		getContentPane().add(panel);
		panel.setLayout(null);

		JLabel lblNewLabel_1 = new JLabel("班级号：");
		lblNewLabel_1.setIcon(new ImageIcon(ClassEditInterfrm.class.getResource("/images/num.png")));
		lblNewLabel_1.setBounds(72, 34, 98, 15);
		panel.add(lblNewLabel_1);

		textField_1 = new JTextField();
		textField_1.setEditable(false);
		textField_1.setBounds(180, 31, 133, 21);
		panel.add(textField_1);
		textField_1.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("所在系：");
		lblNewLabel_2.setIcon(new ImageIcon(ClassEditInterfrm.class.getResource("/images/describe.png")));
		lblNewLabel_2.setBounds(72, 71, 96, 15);
		panel.add(lblNewLabel_2);

		comboBox = new JComboBox();
		comboBox.setBounds(180, 67, 131, 23);
		panel.add(comboBox);

		JLabel lblNewLabel_3 = new JLabel("班级名称：");
		lblNewLabel_3.setIcon(new ImageIcon(ClassEditInterfrm.class.getResource("/images/班级.png")));
		lblNewLabel_3.setBounds(71, 109, 95, 15);
		panel.add(lblNewLabel_3);

		textField_2 = new JTextField();
		textField_2.setBounds(181, 104, 129, 21);
		panel.add(textField_2);
		textField_2.setColumns(10);

		JLabel lblNewLabel_4 = new JLabel("年级：");
		lblNewLabel_4.setIcon(new ImageIcon(ClassEditInterfrm.class.getResource("/images/年级.png")));
		lblNewLabel_4.setBounds(397, 35, 98, 15);
		panel.add(lblNewLabel_4);

		comboBox_1 = new JComboBox();
		comboBox_1.setBounds(486, 30, 133, 23);
		panel.add(comboBox_1);

		JLabel lblNewLabel_5 = new JLabel("人数：");
		lblNewLabel_5.setIcon(new ImageIcon(ClassEditInterfrm.class.getResource("/images/num.png")));
		lblNewLabel_5.setBounds(397, 71, 79, 15);
		panel.add(lblNewLabel_5);

		textField_3 = new JTextField();
		textField_3.setBounds(485, 67, 134, 21);
		panel.add(textField_3);
		textField_3.setColumns(10);

		JButton btnNewButton_1 = new JButton("添加");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (StringUtil.isEmpty(textField_1.getText())) {
					JOptionPane.showMessageDialog(null, "请选择需要修改的数据", "提示", JOptionPane.ERROR_MESSAGE);
					return;
				}

				if (StringUtil.checkLenth(textField_2.getText(), 20)) {
					JOptionPane.showMessageDialog(null, "班级名字太长", "提示", JOptionPane.ERROR_MESSAGE);
					return;
				}
				if (Integer.parseInt(textField_3.getText())>60 || Integer.parseInt(textField_3.getText())<0 ) {
					JOptionPane.showMessageDialog(null, "班级人数在0~60", "提示", JOptionPane.ERROR_MESSAGE);
					textField_3.requestFocus();
					textField_3.selectAll();
					return;
				}
				Classinformation classinformation = ClassinformationDao.select(textField_1.getText());
				if (comboBox.getSelectedItem() instanceof Major) {
					Major major = (Major) comboBox.getSelectedItem();
					classinformation.setM_id(major.getM_id());
				}
				classinformation.setCla_name(textField_2.getText());
				classinformation.setCla_number(textField_3.getText());
				if (ClassinformationDao.updata(classinformation) > 0) {
					JOptionPane.showMessageDialog(null, "修改成功", "提示", JOptionPane.INFORMATION_MESSAGE);
					filltable(ClassinformationDao.select());
					return;
				}
				JOptionPane.showMessageDialog(null, "修改失败", "提示", JOptionPane.ERROR_MESSAGE);
				return;
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(ClassEditInterfrm.class.getResource("/images/add.png")));
		btnNewButton_1.setBounds(153, 159, 93, 23);
		panel.add(btnNewButton_1);

		JButton btnNewButton_2 = new JButton("删除");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (StringUtil.isEmpty(textField_1.getText())) {
					JOptionPane.showMessageDialog(null, "请选择需要删除的数据", "提示", JOptionPane.ERROR_MESSAGE);
					return;
				}
				int res = JOptionPane.showConfirmDialog(null, "是否确定删除");
				if (res == 0) {
					if (ClassinformationDao.delete(textField_1.getText()) > 0) {
						JOptionPane.showMessageDialog(null, "删除成功", "提示", JOptionPane.INFORMATION_MESSAGE);
						textField_1.setText("");
						textField_2.setText("");
						textField_3.setText("");
						comboBox.setSelectedIndex(0);
						comboBox_1.setSelectedIndex(0);
						filltable(ClassinformationDao.select());
						return;
					}
					JOptionPane.showMessageDialog(null, "删除失败", "提示", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnNewButton_2.setIcon(new ImageIcon(ClassEditInterfrm.class.getResource("/images/删 除 .png")));
		btnNewButton_2.setBounds(386, 160, 93, 23);
		panel.add(btnNewButton_2);

		filltable(ClassinformationDao.select());
	}

	private void filltable(ArrayList<Classinformation> arrayList) {
		Major major;
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		dtm.setRowCount(0);
		for (Classinformation classinformation : arrayList) {
			Vector v = new Vector();
			v.add(classinformation.getCla_id());
			major = MajorDao.select(classinformation.getM_id());
			v.add(major.getM_name());
			v.add(classinformation.getCla_name());
			v.add(classinformation.getCla_grade());
			v.add(classinformation.getCla_number());
			dtm.addRow(v);
		}
	}

	/**
	 * 初始化专业下拉框
	 */
	private void fillMajorType() {
		comboBox.addItem("请选择专业");
		ArrayList<Major> arrayList = MajorDao.select();
		for (Major major : arrayList) {
			comboBox.addItem(major);
		}
	}

	private void fillMajorType(String major) {
		ArrayList<Major> arrayList = MajorDao.select();
		for (Major m : arrayList) {
			if (m.getM_id().equals(major)) {
				comboBox.addItem(m);
			}
		}
		for (Major m : arrayList) {
			if (!m.getM_id().equals(major)) {
				comboBox.addItem(m);
			}
		}
	}

	/**
	 * 初始化年级下拉框
	 */
	Integer[] gradelArr = { 0, 0, 0, 0 };
	int year = NumberGeneration.getDate();

	private void fillGradeType() {
		for (int i = 0; i < 4; i++) {
			gradelArr[i] = year - i;
		}
		comboBox_1.addItem("请选择年级");
		for (int i : gradelArr) {
			comboBox_1.addItem(i);
		}
	}

	private void fillGradeType(Integer yearString) {
		for (int i = 0; i < 4; i++) {
			gradelArr[i] = year - i;
		}
		comboBox_1.addItem(yearString);
		for (int i : gradelArr) {
			if (i != yearString) {
				comboBox_1.addItem(i);
			}
		}
	}
}
